import Router from 'vue-router'
import company from './company'
import scheduling from './scheduling'
import launch from './launch'
import material from './material'
import time from './time'
import delivery from './delivery'
import video from './video'
import commerce from './commerce'
import qianchuan from './qianchuan'
import finance from './finance'
import cid from './cid'
import feixing from './feixing'
import task from './task'
import siyu from './siyu'
import tiktok from './tiktok'
import performance from './performance';

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/main'
        },
        {
            path: '/login',
            name: 'login',
            component: resolve => (require(["@/components/user/Login"], resolve))
        },
        {
            path: '/hotellist',
            name: 'hotellist',
            component: resolve => (require(["@/components/hotel/HotelList"], resolve))
        },
        {
            path: '/hoteldetails',
            name: 'hoteldetails',
            component: resolve => (require(["@/components/hotel/HotelDetails"], resolve))
        },
        {
            path: '/launchdata',
            name: 'launchdata',
            component: resolve => (require(["@/components/launch/LaunchData"], resolve))
        },
        {
            path: '/videodata',
            name: 'videodata',
            component: resolve => (require(["@/components/videoplay/Index"], resolve))
        },
        {
            path: '/launchlive',
            name: 'launchlive',
            component: resolve => (require(["@/components/launch/LaunchLive"], resolve))
        },
        {
            path: '/main',
            name: 'main',
            component: resolve => (require(["@/components/Main"], resolve)),
            redirect: '/welcome',
            children:[
                {
                    path: '/welcome',
                    name: '欢迎使用',
                    component: resolve => (require(["@/components/user/Welcome"], resolve))
                },

                // 公司管理
                ...company,
                // 需求排期
                ...scheduling,
                // 投放管理
                ...launch,
                // 素材库
                ...material,
                //时段数据
                ...time,
                // 自动投放
                ...delivery,
                // 视频测试
                ...video,
                //电商罗盘
                ...commerce,
                // 财务流水
                ...finance,
                // 千川管理
                ...qianchuan,
                // 拼多多授权
                ...cid,
                // 飞星数据
                ...feixing,
                // 导出任务
                ...task,
                // 私域数据
                ...siyu,
                // 抖音投放
                ...tiktok,
                //绩效管理
                ...performance
            ]
        },
    ]
})

// 路由跳转配置
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
}

// 路由守卫
router.beforeEach((to, form, next) => {
    const token = window.localStorage.getItem('token')
    // console.log(token)
    if (to.path === '/hotellist' || to.path === '/hoteldetails' || to.path === '/videodata') {
        next();
    }
    else if (to.path !== '/login' && !token) {
        next({ name: 'login' })
        Vue.prototype.$message.error('登录已失效,请重新登录！');
    } else {
        next();
    }
    // if (to.path === '/login') {
    //     window.localStorage.removeItem("token");
    //     window.localStorage.removeItem("userinfo");
    //     next();
    // }
})

export default router;